
/**
* @Title: SimpleWN.java
* @Package com.leetone
* @Description: 【】
* @date 2018年3月7日 下午1:47:09
* @version V1.0
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

package com.leetone.simple;


/**
* @ClassName: SimpleWN
* @Description: 【对象上的wait和notify使用示例】
* @date 2018年3月7日 下午1:47:09
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

public class SimpleWN {
	final static Object obj = new Object();
	
	public static class T1 extends Thread{
		@Override
		public void run() {
			synchronized(obj)
			{
				System.out.println("t1 is start");
				System.out.println("t1 start wait for a moment");
				try {
					//这个方法必须要先获取到obj锁才能够调用，否则会报非法调用
					obj.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("t1 is complete!");
			}
		}
	}
	
	public static class T2 extends Thread{
		@Override
		public void run() {
			synchronized(obj)
			{
				System.out.println("t2 is start !");
				System.out.println("t2 is notify an other thread");
				obj.notify();
				System.out.println("t2 to sleep 2000 ");
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("t2 is complete!");
			}
		}
	}
	/**
	 * @throws InterruptedException 
	* @Title: main
	* @Description: 【】
	* @date 2018年3月7日 下午1:47:09
	* @author:[谭磊][leetone123@163.com]
	* @param @param args    参数
	* @return void    返回类型
	* @throws
	*/

	public static void main(String[] args) throws InterruptedException {
		//这样t1结束必须要等t2先结束后通知到他才能继续往下结束
		Thread t1 = new T1();
		Thread t2 = new T2();
		t1.start();
		Thread.sleep(100);
		t2.start();
	}

}
